/**
 * JAVACC DEMO 1.0
 * @copy right zbxsoft company All rights reserved. 
 * @Package com.zbxsoft.platform.template.helper  
 */
package com.apache.design.template.helper;

import com.apache.design.template.FreemarkerHelper;
import com.apache.design.util.DesignEntity;
import com.apache.design.util.FileOperate;

import java.util.HashMap;
import java.util.Map;

/**
 * description:  生成系统配置实现类
 */
public class SystemConfigTempManager extends SupperTemplateManager {


	@SuppressWarnings("unchecked")
	public boolean doInvoke(DesignEntity entity) {
		boolean mark = false;
		String projectName = entity.getProjectName();
		String xmlPath = entity.getFilePath();
		try {
			xmlPath = xmlPath + "/" + projectName + "/src/main/resources/config/";
			FileOperate.getInstance().newCreateFolder(xmlPath);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("driverClassName", entity.getParam("driverClassName"));
			data.put("jdbcUrl", entity.getParam("jdbcUrl"));
			data.put("jdbcUser", entity.getParam("jdbcUsername"));
			data.put("jdbcPwd", entity.getParam("jdbcPassword"));
			data.put("databaseType", getDatabaseType(entity.getParam("driverClassName")));
			String result = FreemarkerHelper.instance().getTemplateStr("config/jdbc.inc", data);
			FileOperate.getInstance().newCreateFile(xmlPath + "jdbc.properties", result);

			data.put("projectName",projectName);
			data.put("sysEname",entity.getSysEname());
			result = FreemarkerHelper.instance().getTemplateStr("config/config.inc", data);
			FileOperate.getInstance().newCreateFile(xmlPath + "config.properties", result);
			mark = true;
		} catch (Exception e) {
			e.printStackTrace();
			log.error("生成配置失败", e);
		}
		return mark;
	}

	private String getDatabaseType(String driverClassName){
		if ("oracle.jdbc.OracleDriver".equals(driverClassName)) {
			return "oracle";
		} else if ("com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driverClassName)) {
			return "sqlserver";
		} else {
			return "mysql";
		}
	}
}
